package com.x.erp.service;

import com.alibaba.fastjson.JSON;
import com.x.erp.model.Apk;
import com.x.erp.model.Car;
import com.x.erp.model.User;
import com.x.erp.thirdparty.CarMappingThirdparty;
import com.x.erp.util.ConstantState;
import com.x.erp.util.ConstantTopic;
import com.x.framework.exception.BusinessException;
import com.x.framework.service.BaseService;
import com.x.rocketmq.RocketMqProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

//import com.x.jpush.util.JPushClientUtil;
//import com.x.kafka.XKafkaTemplate;
//import org.springframework.kafka.support.SendResult;

@Component
public class MqService extends BaseService {
    private static final Logger logger = LoggerFactory.getLogger(MqService.class);

//    @Autowired
//    private XKafkaTemplate xKafkaTemplate;

    @Autowired
    private RocketMqProducer rocketMqProducer;

    @Autowired
    private QczjService qczjService;// 汽车之家服务

    @Autowired
    private XyService xyService;// 行圆服务

    @Autowired
    private FirstCarService firstCarService;// 第一车网服务

    @Autowired
    private SouHuCarService souHuCarService;// 搜狐汽车服务

    @Autowired
    private CarService carService;// 车辆服务

    @Autowired
    private CarMappingThirdpartyService carMappingThirdpartyService;// 第三方车辆服务

    @Autowired
    private ZhiTongService zhiTongService;// 智通服务

//    @Autowired
//    private JPushClientUtil jpushClientUtil;// 极光推送服务

    @PostConstruct
    public void testKafkaProducer() {
        try {
            Apk apk = new Apk();
            apk.setApkId(getUid(5));
            apk.setApkKey(getUid(20));
//            ListenableFuture<SendResult<String, String>> listenableFuture = xKafkaTemplate.produceMessage("ERP.TEST", apk);
//            logger.info("KAFKA ERP.TEST SUCCESS");

            boolean flag = rocketMqProducer.produceMessage(ConstantTopic.TOPIC_ERP_CAR, ConstantTopic.TAG_TEST, apk);
            logger.info("RocketMq---------------------------------------RocketMq produce ERP_CAR.TEST " + flag);
        } catch (Exception e) {
            throw new BusinessException(e);
        }
    }

    public void testKafkaConsumer(Apk apk) {
        logger.info("Kafka ERP.TEST consume SUCCESS " + apk.getApkId());
    }

    public void testRocketMqConsumer(Apk apk) {
        logger.info("RocketMq---------------------------RocketMq testRocketMqConsumer" + ConstantTopic.TOPIC_ERP_CAR + "." + ConstantTopic.TAG_TEST + " consume SUCCESS " + apk.getApkId());
    }

    /**
     * 车辆上架
     *
     * @param car
     */
    public void updateCarSaleState20(Car car) {
        logger.info("rmq-message:车辆上架，通知汽车之家（start）：" + JSON.toJSONString(car));
        qczjService.insertCarQczj(car);
        logger.info("rmq-message:车辆上架，通知汽车之家（end）：" + JSON.toJSONString(car));

        logger.info("rmq-message:车辆上架，通知行圆（start）：" + JSON.toJSONString(car));
        xyService.insertCarXy(car);
        logger.info("rmq-message:车辆上架，通知行圆（end）：" + JSON.toJSONString(car));

        logger.info("rmq-message:车辆上架，通知第一车网（start）：" + JSON.toJSONString(car));
        firstCarService.insertOrUpdateCarFirst(car);
        logger.info("rmq-message:车辆上架，通知第一车网（end）：" + JSON.toJSONString(car));

        logger.info("rmq-message:车辆上架，通知搜狐汽车（start）：" + JSON.toJSONString(car));
        souHuCarService.updateCarSaleState20(car);
        logger.info("rmq-message:车辆上架，通知搜狐汽车（end）：" + JSON.toJSONString(car));
    }

    /**
     * 车辆下架
     *
     * @param car
     */
    public void updateCarSaleState30(Car car) {
        logger.info("rmq-message:车辆下架，通知汽车之家（start）：" + JSON.toJSONString(car));
        qczjService.updateCarSaleState30(car);
        logger.info("rmq-message:车辆下架，通知汽车之家（end）：" + JSON.toJSONString(car));

        logger.info("rmq-message:车辆下架，通知行圆（start）：" + JSON.toJSONString(car));
        xyService.updateCarSaleState30(car);
        logger.info("rmq-message:车辆下架，通知行圆（end）：" + JSON.toJSONString(car));

        CarMappingThirdparty carMappingThirdparty = new CarMappingThirdparty();
        carMappingThirdparty.setCarId(car.getCarId());
        carMappingThirdparty.setCarType(1);
        List<CarMappingThirdparty> carMappingThirdpartyListFirst = carMappingThirdpartyService.selectCarMappingThirdpartList(carMappingThirdparty);
        if (carMappingThirdpartyListFirst != null && carMappingThirdpartyListFirst.size() > 0){
            logger.info("rmq-message:车辆下架，通知第一车网（start）：" + JSON.toJSONString(car));
            firstCarService.insertOrUpdateCarFirst(car);
            logger.info("rmq-message:车辆下架，通知第一车网（end）：" + JSON.toJSONString(car));
        }

        carMappingThirdparty.setCarType(2);
        List<CarMappingThirdparty> carMappingThirdpartyListSouHu = carMappingThirdpartyService.selectCarMappingThirdpartList(carMappingThirdparty);
        if (carMappingThirdpartyListSouHu != null && carMappingThirdpartyListSouHu.size() > 0){
            logger.info("rmq-message:车辆下架，通知搜狐汽车（start）：" + JSON.toJSONString(car));
            souHuCarService.updateCarSaleState30(car);
            logger.info("rmq-message:车辆下架，通知搜狐汽车（end）：" + JSON.toJSONString(car));
        }
    }

    /**
     * 车辆出库
     *
     * @param car
     */
    public void updateCarStockState40(Car car) {
        logger.info("rmq-message:车辆出库，通知汽车之家（start）：" + JSON.toJSONString(car));
        qczjService.updateCarStockState40(car);
        logger.info("rmq-message:车辆出库，通知汽车之家（end）：" + JSON.toJSONString(car));

        logger.info("rmq-message:车辆出库，通知行圆（start）：" + JSON.toJSONString(car));
        xyService.updateCarStockState40(car);
        logger.info("rmq-message:车辆出库，通知行圆（end）：" + JSON.toJSONString(car));

        CarMappingThirdparty carMappingThirdparty = new CarMappingThirdparty();
        carMappingThirdparty.setCarId(car.getCarId());
        carMappingThirdparty.setCarType(1);
        List<CarMappingThirdparty> carMappingThirdpartyList = carMappingThirdpartyService.selectCarMappingThirdpartList(carMappingThirdparty);
        if (carMappingThirdpartyList != null && carMappingThirdpartyList.size() > 0){
            logger.info("rmq-message:车辆出库，通知第一车网（start）：" + JSON.toJSONString(car));
            firstCarService.insertOrUpdateCarFirst(car);
            logger.info("rmq-message:车辆出库，通知第一车网（end）：" + JSON.toJSONString(car));
        }

        carMappingThirdparty.setCarType(2);
        List<CarMappingThirdparty> carMappingThirdpartyListSouHu = carMappingThirdpartyService.selectCarMappingThirdpartList(carMappingThirdparty);
        if (carMappingThirdpartyListSouHu != null && carMappingThirdpartyListSouHu.size() > 0){
            logger.info("rmq-message:车辆出库，通知搜狐汽车（start）：" + JSON.toJSONString(car));
            souHuCarService.updateCarStockState40(car);
            logger.info("rmq-message:车辆出库，通知搜狐汽车（end）：" + JSON.toJSONString(car));
        }
    }

    /**
     * 车辆修改
     *
     * @param car
     */
    public void updateCar(Car car) {

        Car carBack = carService.selectCar(car);
        if (carBack != null && carBack.getCarSaleState() != null && carBack.getCarSaleState() == ConstantState.CAR_SALE_STATE_ONSALE_FINISH && carBack.getCarStockState() == ConstantState.CAR_STOCK_STATE_IN_FINISH){
            logger.info("rmq-message:车辆信息修改，通知行圆（start）：" + JSON.toJSONString(carBack));
            xyService.updateCarXy(carBack);
            logger.info("rmq-message:车辆信息修改，通知行圆（end）：" + JSON.toJSONString(carBack));

            logger.info("rmq-message:车辆修改，通知第一车网（start）：" + JSON.toJSONString(carBack));
            firstCarService.insertOrUpdateCarFirst(carBack);
            logger.info("rmq-message:车辆修改，通知第一车网（end）：" + JSON.toJSONString(carBack));

            logger.info("rmq-message:车辆修改，通知搜狐汽车（start）：" + JSON.toJSONString(carBack));
            souHuCarService.updateCarSaleState20(carBack);
            logger.info("rmq-message:车辆修改，通知搜狐汽车（end）：" + JSON.toJSONString(carBack));
        }
    }

    /**
     * 车辆删除
     *
     * @param car
     */
    public void deleteCar(Car car) {
        logger.info("rmq-message:车辆信息删除，通知行圆（start）：" + JSON.toJSONString(car));
        xyService.deleteCarXy(car);
        logger.info("rmq-message:车辆信息删除，通知行圆（end）：" + JSON.toJSONString(car));

        CarMappingThirdparty carMappingThirdparty = new CarMappingThirdparty();
        carMappingThirdparty.setCarId(car.getCarId());
        carMappingThirdparty.setCarType(2);
        List<CarMappingThirdparty> carMappingThirdpartyList = carMappingThirdpartyService.selectCarMappingThirdpartList(carMappingThirdparty);
        if (carMappingThirdpartyList != null && carMappingThirdpartyList.size() > 0){
            logger.info("rmq-message:车辆信息删除，通知搜狐汽车（start）：" + JSON.toJSONString(car));
            souHuCarService.deleteCarSouHu(car);
            logger.info("rmq-message:车辆信息删除，通知搜狐汽车（end）：" + JSON.toJSONString(car));
        }
    }

    /**
     * 新增商户信息
     * @param user
     */
    public void insertShop(User user){
        logger.info("rmq-message:新增商户，通知智通（start）：" + JSON.toJSONString(user));
        zhiTongService.insertShop(user);
        logger.info("rmq-message:新增商户，通知智通（end）：" + JSON.toJSONString(user));
    }

}
